package com.example.ordersystem.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.ordersystem.R;
import com.example.ordersystem.pojo.Food;
import com.example.ordersystem.pojo.Order;

import java.util.List;

/*
 * 适配器展示历史订单（listView）
 */
public class OldOrderAdapter extends ArrayAdapter {

    private final int resourceId;

    public OldOrderAdapter(Context context, int textViewResourceId, List<Order> listItems) {
        super(context, textViewResourceId, listItems);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Order order = (Order) getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(resourceId, null);

        int imageId = 0;
        ImageView foodImage = view.findViewById(R.id.oo_food_image);

        final TextView foodName = view.findViewById(R.id.oo_food_name);
        TextView foodMessage = view.findViewById(R.id.oo_food_message);
        TextView foodPrice = view.findViewById(R.id.oo_food_price);
        TextView foodNum = view.findViewById(R.id.oo_food_num);
        TextView storeName = view.findViewById(R.id.oo_store_name);
        TextView otime = view.findViewById(R.id.oo_time);

        List<Food> menuList = ListDataSave.getMenuList(getContext(), order.getStoreName());
        for (Food food : menuList) {
            if(food.getFoodName().equals(order.getFoodName())){
                imageId = food.getImageId();
            }
        }

        foodImage.setImageResource(imageId);
        storeName.setText("店铺名："+order.getStoreName());
        foodName.setText("菜品名：" +order.getFoodName());
        foodMessage.setText("菜品介绍："+order.getFoodMessage());
        foodPrice.setText("价格：" +(order.getFoodPrice()*order.getFoodNum())+"元");
        foodNum.setText("数量为："+order.getFoodNum());
        otime.setText("时间为："+order.getOtime());


        return view;
    }
}